package integraltask

import (
	"context"
	"strings"
	"time"

	"mall/common/modelx"
	"mall/service/backend/common/errorx"
	"mall/service/backend/common/i18n"
	"mall/service/setting/rpc/internal/svc"
	"mall/service/setting/rpc/internal/utils/errorhandler"
	"mall/service/setting/rpc/types/setting"

	"github.com/zeromicro/go-zero/core/logx"
)

type BackendUpdateIntegralTaskLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewBackendUpdateIntegralTaskLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BackendUpdateIntegralTaskLogic {
	return &BackendUpdateIntegralTaskLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *BackendUpdateIntegralTaskLogic) BackendUpdateIntegralTask(in *setting.IntegralTaskInfo) (*setting.BaseResp, error) {
	logx.Infof("BackendUpdateIntegralTask : %+v ", in)
	now := time.Now()
	if len(in.Ids) > 0 { // 批量修改状态
		tx, err := l.svcCtx.DB.BeginTx(l.ctx, nil)
		if err != nil {
			return nil, errorhandler.DefaultRepositoryError(l.Logger, err, in)
		}
		for _, id := range in.Ids {
			if id > 0 && in.Status != nil {
				taski, err := l.svcCtx.IntegralTask.FindOneNoCache(l.ctx, tx, id)
				if err != nil {
					return nil, errorhandler.DefaultRepositoryError(l.Logger, err, in)
				}

				taski.Status = uint8(*in.Status)
				taski.UpdatedAt = now
				err = l.svcCtx.IntegralTask.Update(l.ctx, tx, taski)
				if err != nil {
					tx.Rollback()
					return nil, errorhandler.DefaultRepositoryError(l.Logger, err, in)
				}
			}
		}

		tx.Commit()

		return &setting.BaseResp{
			Msg: i18n.UpdateSuccess,
		}, nil
	}
	if in.Id == nil || *in.Id == 0 {
		return nil, errorx.NewInvalidArgumentError(i18n.ValidationError)
	}

	id := *in.Id
	task, err := l.svcCtx.IntegralTask.FindOneNoCache(l.ctx, nil, id)
	if err != nil {
		return nil, errorhandler.DefaultRepositoryError(l.Logger, err, in)
	}

	if in.RuleName != nil && len(strings.TrimSpace(*in.RuleName)) == 0 {
		return nil, errorx.NewInvalidArgumentError("forum.postTitleRequired")
	}
	if in.RuleSlug != nil && len(strings.TrimSpace(*in.RuleSlug)) > 0 {
		task.RuleSlug = *in.RuleSlug
	}

	if in.RuleType != nil && *in.RuleType > 0 {
		task.RuleType = uint16(*in.RuleType)
	}
	if in.PicId != nil && *in.PicId > 0 {
		task.PicID = *in.PicId
	}
	if in.PicUrl != nil && len(strings.TrimSpace(*in.PicUrl)) > 0 {
		task.PicURL = *in.PicUrl
	}
	if in.Frequency != nil && *in.Frequency > 0 {
		task.Frequency = uint16(*in.Frequency)
	}
	if in.Points != nil && *in.Points > 0 {
		task.Points = *in.Points
	}
	if in.EffectiveTime != nil && len(strings.TrimSpace(*in.EffectiveTime)) > 0 {
		task.EffectiveTime, err = modelx.ParseLocalTime(*in.EffectiveTime)
		if err != nil {
			return nil, err
		}
	}
	if in.Sort != nil && *in.Sort > 0 {
		task.Sort = uint16(*in.Sort)
	}
	if in.Ip != nil && len(strings.TrimSpace(*in.Ip)) > 0 {
		task.IP = *in.Ip
	}
	if in.Description != nil && len(strings.TrimSpace(*in.Description)) > 0 {
		task.Description = modelx.GetNullString(*in.Description)
	}
	if in.Status != nil && *in.Status > 0 {
		task.Status = uint8(*in.Status)
	}

	task.UpdatedAt = now

	err = l.svcCtx.IntegralTask.Update(l.ctx, nil, task)
	if err != nil {
		return nil, errorhandler.DefaultRepositoryError(l.Logger, err, in)
	}
	return &setting.BaseResp{
		Msg: i18n.UpdateSuccess,
	}, nil
}
